﻿using System;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Administration.Data;
using ConfigurationManagement.Data;
using Environment = ConfigurationManagement.Data.Environment;

namespace ConfigurationManagement.Administration.UI.Utilities
{
    public partial class CopyProjectForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Project _selectedProject;

        public CopyProjectForm()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private static void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void CopyProjectFormLoad(object sender, EventArgs e)
        {
            LoadProjects();
        }

        private void CopyProjectButtonClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(NewNameTextBox.Text))
            {
                MessageBox.Show(@"Please supply a new solution name");
                return;
            }

            if (_selectedProject == null)
            {
                MessageBox.Show(@"Please select an existing project");
                return;
            }
            if (_db.Projects.Count(p => p.ProjectName == NewNameTextBox.Text) > 0)
            {
                MessageBox.Show(@"A project with this name already exists");
                return;
            }

            try
            {
                CopyProject();
                MessageBox.Show(@"The solution has been copied");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        private void LoadProjects()
        {
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DataSource = _db.Projects;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
        }

        private void CopyProject()
        {
            var newProject = new Project
                                 {
                                     ProjectName = NewNameTextBox.Text,
                                     ProjectDescription = _selectedProject.ProjectDescription
                                 };

            //Add environments
            foreach (var newEnvironment in _selectedProject.Environments.Select(environment => new Environment
                                                                                                   {
                                                                                                       EnvironmentDEscription = environment.EnvironmentDEscription, EnvironmentName = environment.EnvironmentName
                                                                                                   }))
            {
                newProject.Environments.Add(newEnvironment);
            }

            //Add solutions
            foreach (var solution in _selectedProject.Solutions)
            {
                var newSolution = new Solution
                                      {
                                          SolutionName = solution.SolutionName,
                                          SolutionDescription = solution.SolutionDescription
                                      };
                foreach (var setting in solution.SolutionSettings)
                {
                    var newSetting = new SolutionSetting
                                         {
                                             DefaultValue = setting.DefaultValue,
                                             SettingDescription = setting.SettingDescription,
                                             SettingTag = setting.SettingTag
                                         };

                    foreach (var newSettingValue in setting.SettingValues.Select(settingValue => new SettingValue
                                                                                                     {
                                                                                                         EnvironmentID = settingValue.EnvironmentID, SettingValue1 = settingValue.SettingValue1, SolutionSettingID = settingValue.SolutionSettingID
                                                                                                     }))
                    {
                        newSetting.SettingValues.Add(newSettingValue);
                    }
                    newSolution.SolutionSettings.Add(newSetting);
                }
                newProject.Solutions.Add(newSolution);
            }

            //Add Project
            _db.Projects.InsertOnSubmit(newProject);
            if (DataContext.ContainsChanges(_db))
                _db.SubmitChanges();
        }
    }
}